package ar.com.photo_admin.domain

import java.util.Set

class EventPhoto extends DomainObject {
   
	String name
	
	/**
	 * URL of the photo on the server.
	 */
	String url
	
	/**
	 * URL of the photo's thumbnail on the server.
	 */
	String thumbnailUrl

	/**
	 * URL of the photo's medium size thumbnail on the server.
	 */
	String thumbnailMediumUrl

	/**
	 * Location of the photo in the server	
	 */
	String pathInServer

	/**
	 * 	Location of the thumbnail in the server
	 */ 
	String thumbnailPathInServer

	/**
	 * 	Location of the medium size thumbnail in the server
	 */
	String thumbnailMediumPathInServer
	
	/**
	 * URL of the photo on the external server.
	 */
	String urlInHost
	
	/**
	 * URL of the photo's thumbnail on the external server.
	 */
	String thumbnailUrlInHost
	
	/**
	 * URL of the photo's medium size thumbnail on the external server.
	 */
	String thumbnailMediumUrlInHost
	
	/**
	 * User that upload the photo
	 */
	User user

	Event event
	
	Set photoInAlbums
		
	long temporalEventNumber
	
	boolean toBeDeleted
		
	boolean hosted = false
	
	int height
	
	int width
	
	static hasMany = [photoInAlbums:PhotoInAlbum]
	
	static belongsTo = [Event]
			
	static constraints = {
		name(blank:false) 
		url(blank:false)
		thumbnailUrl(blank:false)
		thumbnailMediumUrl(blank:false)
		event(nullable:true)
		pathInServer(blank:false)
		temporalEventNumber()
		thumbnailPathInServer(blank:false)
		thumbnailMediumPathInServer(blank:false)
		urlInHost(nullable:true)
		thumbnailUrlInHost(nullable:true)
		thumbnailMediumUrlInHost(nullable:true)
		user(nullable:true)
	}
	
	static mapping = {		
		event cascade : "save-update"	
	}
	
	List getAlbums() {
		return photoInAlbums.collect{ it.album }
	}
	
	List addToAlbums(EventAlbum album) { 
		PhotoInAlbum.link(this, album) 
		return getAlbums() 		
	}
	
	List removeFromAlbums(EventAlbum album) {		
		PhotoInAlbum.unlink(this, album) 
		return getAlbums() 	
	}
	
	boolean equals(o) {
		if ( ! o instanceof EventPhoto ) {
		   return false
		}
				
		return o.id == this.id
	}
	
	def getPublishedUrl() {
		return this.hosted ?  this.urlInHost : this.url  
	}
	
	def getPublishedThumbnailUrl() {
		return this.hosted ? this.thumbnailUrlInHost : this.thumbnailUrl 
	}
	
	def getPublishedThumbnailMediumUrl() {
		return this.hosted ? thumbnailMediumUrlInHost : this.thumbnailMediumUrl 
	}
	
	def isVertical(){
		return height > width
	}
	
	String toString(){
		return name
	}
}
